package com.fenghongzhang.day008view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class MyCircle extends View {

    //第一个圆
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    //画文字
    Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);

    //画弧
    Paint paintHu = new Paint(Paint.ANTI_ALIAS_FLAG);

    //time
    int time = 5;
    //进度
    int progress = 0;

    public MyCircle(Context context) {
        super(context);
    }

    public MyCircle(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        paint.setStyle(Paint.Style.STROKE);
//        paint.setStrokeWidth(15);
//        canvas.drawCircle(200,200,150,paint);
//
//
//        paintText.setTextAlign(Paint.Align.CENTER);
//        paintText.setColor(Color.RED);
//        paintText.setTextSize(20);
//        canvas.drawText("哈哈",300,300,paintText);
//
//
//        RectF rectF = new RectF(200, 400, 800, 800);
//        canvas.drawRect(rectF,paint);
//        canvas.drawArc(rectF,270,90,true,paint);


        //画第一个底圆
        RectF rectF = new RectF(200, 200, 400, 400);
//        canvas.drawRect(rectF,paint);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(15);
        canvas.drawArc(rectF,0,360,false,paint);

        //画第二描边的源
        paintHu.setStyle(Paint.Style.STROKE);
        paintHu.setStrokeWidth(15);
        paintHu.setColor(Color.RED);
        canvas.drawArc(rectF,270,progress,false,paintHu);

        //画文字
        paintText.setTextAlign(Paint.Align.CENTER);
        paintText.setColor(Color.RED);
        paintText.setTextSize(50);
        canvas.drawText(time+"",rectF.centerX(),rectF.centerY()+25,paintText);

    }

    public void statr(){

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <= 360; i++) {
                    progress = i;
                    //刷新当前的view
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if(i % 72 == 0){
                        time--;
                    }
                    //刷新
                    MyCircle.this.postInvalidate();
                }
            }
        }).start();

    }
}
